package com.wyl.selenium.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.BeanCopier;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyl.selenium.bean.LabelNode;
import com.wyl.selenium.common.CommonPage;
import com.wyl.selenium.dto.ProjectBaseInfoDto;
import com.wyl.selenium.entity.ProjectBaseInfo;
import com.wyl.selenium.framework.BaseService;
import com.wyl.selenium.framework.BaseServiceImpl;
import com.wyl.selenium.mapper.ProjectBaseInfoMapper;
import com.wyl.selenium.req.ProjectBaseInfoReq;
import com.wyl.selenium.service.ProjectBaseInfoService;
import com.wyl.selenium.vo.LabelNodeVo;
import com.wyl.selenium.vo.ProjectBaseInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author WuYiLong
 * @Date 2023/11/17 11:55
 */
@Service
public class ProjectBaseInfoServiceImpl extends BaseServiceImpl<ProjectBaseInfo> implements ProjectBaseInfoService {

    @Override
    public ProjectBaseInfoVo getProjectBaseInfoById(Long id) {
        ProjectBaseInfo projectBaseInfo = getById(id);
        ProjectBaseInfoVo projectBaseInfoVo = BeanUtil.copyProperties(projectBaseInfo, ProjectBaseInfoVo.class);
        return projectBaseInfoVo;
    }

    @Override
    public List<ProjectBaseInfoVo> listProjectBaseInfo(List<Long> ids) {
        List<ProjectBaseInfo> projectBaseInfos = listByIds(ids);
        List<ProjectBaseInfoVo> projectBaseInfoVos = projectBaseInfos.stream().map(m -> {
            ProjectBaseInfoVo projectBaseInfoVo = new ProjectBaseInfoVo();
            BeanUtil.copyProperties(m, projectBaseInfoVo);
            return projectBaseInfoVo;
        }).collect(Collectors.toList());
        return projectBaseInfoVos;
    }

    @Override
    public List<LabelNodeVo> listLabelNode() {
        List<ProjectBaseInfo> list = list();
        List<LabelNodeVo> labelNodeVos = list.stream().map(m -> {
            LabelNodeVo labelNodeVo = new LabelNodeVo();
            labelNodeVo.setLabel(m.getName());
            labelNodeVo.setValue(m.getId().toString());
            return labelNodeVo;
        }).collect(Collectors.toList());
        return labelNodeVos;
    }

    @Override
    public CommonPage<List<ProjectBaseInfoVo>> pageProjectBaseInfo(ProjectBaseInfoReq projectBaseInfoReq) {
        Page<ProjectBaseInfo> projectBaseInfoPage = new Page<>(projectBaseInfoReq.getCurrentPage(), projectBaseInfoReq.getPageSize());
        LambdaQueryWrapper<ProjectBaseInfo> wrapper = Wrappers.lambdaQuery(ProjectBaseInfo.class);
        Opt.ofBlankAble(projectBaseInfoReq.getName()).ifPresent(v->wrapper.like(ProjectBaseInfo::getName,v));
        Page<ProjectBaseInfo> page = baseMapper.selectPage(projectBaseInfoPage, wrapper);
        List<ProjectBaseInfoVo> projectBaseInfoVos = page.getRecords().stream().map(m -> {
            ProjectBaseInfoVo projectBaseInfoVo = new ProjectBaseInfoVo();
            BeanUtils.copyProperties(m, projectBaseInfoVo);
            return projectBaseInfoVo;
        }).collect(Collectors.toList());

        CommonPage<List<ProjectBaseInfoVo>> commonPage = new CommonPage(projectBaseInfoReq.getCurrentPage(),projectBaseInfoReq.getPageSize());
        commonPage.setTotal(page.getTotal());
        commonPage.setList(projectBaseInfoVos);
        return commonPage;
    }

    @Override
    public void saveProjectBaseInfo(ProjectBaseInfoDto projectBaseInfoDto) {
        if(projectBaseInfoDto.getId() != null) {
            ProjectBaseInfo projectBaseInfo = getById(projectBaseInfoDto.getId());
            BeanUtil.copyProperties(projectBaseInfoDto,projectBaseInfo);
            saveEntity(projectBaseInfo);
        }else{
            ProjectBaseInfo projectBaseInfo = new ProjectBaseInfo();
            BeanUtils.copyProperties(projectBaseInfoDto,projectBaseInfo);
            saveEntity(projectBaseInfo);
        }
    }

    @Override
    public void deleteProjectBaseInfo(List<Long> ids) {
        removeByIds(ids);
    }
}
